package com.newasia.management.controller;/**
 * Created by mic on 2019/8/1.
 */

import com.newasia.commons.result.ResponseData;
import com.newasia.commons.result.ResponseUtil;
import com.newasia.management.IManagementBrandService;
import com.newasia.management.IManagementSpecService;
import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.dto.productBrand.*;
import com.newasia.management.dto.productSpec.*;
import com.newasia.management.dto.productSpecValue.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * lyh
 * 2019/10/24
 */
@Slf4j
@RestController
@RequestMapping("/management")
public class SpecController {

    @Reference(timeout = 3000)
    IManagementSpecService managementSpecService;


    /**商品规格样式**/
    /**增-父*/
    @PostMapping("/spec")
    public ResponseData createSpec(@RequestBody CreateItemSpecRequest request, HttpServletRequest httpServletRequest){

        CreateItemSpecResponse response= managementSpecService.createItemSpec(request);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }

    /**增-子*/
    @PostMapping("/specValue")
    public ResponseData createSpecValue(@RequestBody CreateSpecValueRequest request, HttpServletRequest httpServletRequest){

        CreateSpecValueResponse response= managementSpecService.createSpecValue(request);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }

    /**删-父*/
    @DeleteMapping("/spec/{specId}")
    public ResponseData deleteSpec(@PathVariable("specId") Long specId, HttpServletRequest httpServletRequest){

        DeleteItemSpecResponse response= managementSpecService.deleteitemSpec(specId);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }

    /**删-子*/
    @DeleteMapping("/specValue/{specValueId}")
    public ResponseData deleteSpecValue(@PathVariable("specValueId") Long specValueId, HttpServletRequest httpServletRequest){

        DeleteSpecValueResponse response= managementSpecService.deleteSpecValue(specValueId);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }


    /**改-父*/
    @PutMapping("/spec/{specId}")
    public ResponseData changeSpec(@PathVariable("specId") Long specId, @RequestBody ChangeItemSpecRequest request, HttpServletRequest httpServletRequest){

        ChangeItemSpecResponse response= managementSpecService.changeItemSpec(specId,request);


        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }

    /**改-子*/
    @PutMapping("/specValue/{specValueId}")
    public ResponseData changeSpec(@PathVariable("specValueId") Long specValueId, @RequestBody ChangeSpecValueRequest request, HttpServletRequest httpServletRequest){

        ChangeSpecValueResponse response= managementSpecService.changeSpecValue(specValueId,request);


        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }

    /**查*/
    @GetMapping("/spec")
    public ResponseData Spec(String key,HttpServletRequest httpServletRequest){
        GetSpecResponse response= managementSpecService.getSpecList(key);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }

    /**查*/
    @GetMapping("/specValue")
    public ResponseData Spec(Long id,HttpServletRequest httpServletRequest){
        GetSpecValueResponse response= managementSpecService.getSpecValueList(id);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }
    
}
